package edu.nyu.msis.utilities;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import edu.nyu.msis.ds.MachineInstruction;
import edu.nyu.msis.ds.Module;
import edu.nyu.msis.ds.Program;
import edu.nyu.msis.ds.SymbolTable;

//import java.io.FileReader;
//import java.io.FileWriter;
//import java.io.IOException;
//
//public class CopyCharacters {
//    public static void main(String[] args) throws IOException {
//        FileReader inputStream = null;
//        FileWriter outputStream = null;
//
//        try {
//            inputStream = new FileReader("xanadu.txt");
//            outputStream = new FileWriter("characteroutput.txt");
//
//            int c;
//            while ((c = inputStream.read()) != -1) {
//                outputStream.write(c);
//            }
//        } finally {
//            if (inputStream != null) {
//                inputStream.close();
//            }
//            if (outputStream != null) {
//                outputStream.close();
//            }
//        }
//    }
//}

public class FileIO {
	String inputFileName;
	String outputFileName;
	FileReader inputStream;
	BufferedWriter outputStream;
	String buffer;
	Integer pos;
	
	public FileIO() {
		super();
		buffer =
		"1 xy 2"+
		" 2 z xy"+
		" 5 R 1004  I 5678  E 2000  R 8002  E 7001"+
		" 0"+
		" 1 z"+
		" 6 R 8001  E 1000  E 1000  E 3000  R 1002  A 1010"+
		" 0"+
		" 1 z"+
		" 2 R 5001  E 4000"+
		" 1 z 2"+
		" 2 xy z"+
		" 3 A 8000  E 1001  E 2000\0";
		pos = 0;
	}
	
	public FileIO(String inputFileName, String outputFileName) {
		pos = 0;
		this.inputFileName = inputFileName;
		this.outputFileName = outputFileName;
		 try {
           inputStream = new FileReader(this.inputFileName);
           outputStream = new BufferedWriter(new FileWriter(this.outputFileName));
		 }
		 catch(Exception e) {
			 System.out.println(e);
		 }
	}
	
	public Character readOneChar()
	{
		int returnVar;
		/*buffer ="1 xy 2"+
				" 2 z xy"+
				" 5 R 1004  I 5678  E 2000  R 8002  E 7001"+
				" 0"+
				" 1 z"+
				" 6 R 8001  E 1000  E 1000  E 3000  R 1002  A 1010"+
				" 0"+
				" 1 z"+
				" 2 R 5001  E 4000"+
				" 1 z 2"+
				" 2 xy z"+
				" 3 A 8000  E 1001  E 2000\0";
		
		returnVar = buffer.charAt(pos);
		pos++;
		return returnVar;*/
		try {
			returnVar = inputStream.read();
			pos++;
			if (returnVar == -1) {
				inputStream.close();
				return '\0';
			}
			else
				return (char)returnVar;
		}
		catch(IOException e) {
			System.out.println(e);
			return '\0';
		}
	}

	public void printSymbolTable(SymbolTable symTable) {
		HashMap<String,Integer> symbolTableVars;
		symbolTableVars = symTable.getMyTable();
		try {
			outputStream.write("Symbol Table\n");
			System.out.println("Symbol Table");
			for (Iterator<Map.Entry<String, Integer>> i= symbolTableVars.entrySet().iterator(); i.hasNext(); )
			{
				Map.Entry<String, Integer> entry= i.next();
				System.out.println(entry.getKey()+"="+entry.getValue());
				outputStream.write(entry.getKey()+"="+entry.getValue()+"\n");
			}
			outputStream.write("\n\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void printProgram(Program program) {
		int i = 0;
		Iterator<Module> it = program.getModules().iterator();
		try {
			outputStream.write("Memory Map\n");
			System.out.println("Memory Map");
			while (it.hasNext()) {
				Module module = it.next();
				Iterator<MachineInstruction> it2 = module.getInstructions().iterator();
				while (it2.hasNext()) {
					MachineInstruction instruct = it2.next();
					String line = String.format("%d: %d%03d\n", i, instruct.getOperand(), instruct.getAddress());
					i++;
					System.out.println(line);
					outputStream.write(line);
				}
			}
			outputStream.write("\n");
		}
		catch (IOException e) {
			System.out.println(e);
		}
	}

	public void printErrors(String errorType, Vector<String> messages) {
		Iterator<String> it = messages.iterator();
		try {
			while (it.hasNext()) {
				String msg = it.next();
				outputStream.write(errorType+": "+msg+"\n");
			}
		}
		catch (IOException e) {
			System.out.println(e);
		}
	}
	
	public void closeFiles() {
		try {
			inputStream.close();
			outputStream.close();
		}
		catch (IOException e) {
			System.out.println(e);
		}
	}
}
